﻿// ---------------------------------------------------------------------
// Partial của lớp SSupervisor
//
// Nội dung:
//    + Các hàm xử lý lúc bắt đầu game
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using System.Threading;
using System.IO;


namespace ServerYuGi
{
    public partial class SSupervisor
    {
        /// <summary>
        /// Thiết lập các giá trị ban đầu để bắt đầu game
        /// </summary>
        public void Init()
        {


            for (int i = 0; i < Players.Count; i++)
            {
                Players[i].SoDiem = Game.SoDiemBatDau;
            }
            int dem = 1;            
            foreach (SPlayer player in Players)
            {             
              
                foreach (SCard card in player.SoBaiChuaChia)
                {
                    card.MyYBaseCard.ID = dem;
                    dem++;
                }
            }

            Table.Players = Players;
        }

        /// <summary>
        /// Bắt đầu ván mới
        /// </summary>
        public void BatDauVan()
        {
            //F5();
            Init();          
            Game.isPlaying = true;         
            TurnManager.IDPlayerTrongLuot = Players[0].ID;      
            SendMessage_GAME_BEGIN_ToID(Players[0].ID, Players[0].ID, Players[1].ID);           
            SendMessage_GAME_BEGIN_ToID(Players[1].ID, Players[1].ID, Players[0].ID);
            SendListCardInDeck(Players[0]);
            SendListCardInDeck(Players[1]);
            ChiaBai();
        }

        public void SendListCardInDeck(SPlayer player)
        {
            List<byte> data = new List<byte>();
            data.Add((byte)player.SoBaiChuaChia.Count);
            foreach (SCard item in player.SoBaiChuaChia)
            {
                data.AddRange(BitConverter.GetBytes(item.MyYBaseCard.Card_TypeID));
                data.Add((byte)item.MyYBaseCard.ID);
            }
            SendMessage_CARD_INDECK_ToID(player.ID, data.ToArray());

        }

        #region Ham Chia Bai Chinh
        List<int> p0_cards,p1_cards;
        public void Add_Simulate_CardInHand(List<int> p0_cards, List<int> p1_cards)
        {
            this.p0_cards = p0_cards;
            this.p1_cards = p1_cards;
        }

        void Fake_ParallelChiaBai(SPlayer p)
        {
             int tmp = 0;
            List<int> fake_listcard = p.ID ==0 ? p0_cards : p1_cards;
            List<byte> param = new List<byte>();
            string Str = " Player " + p.ID;
            //-----------------------------------------------//
            for (int i = 0; i < Game.SoLaChiaBanDau; i++)
            {

                SCard card = p.LayCardTrongBoBai(fake_listcard[i]);
                param.AddRange(BitConverter.GetBytes(card.MyYBaseCard.Card_TypeID));
                param.Add((byte)card.MyYBaseCard.ID);
                p.SoBaiTrenTay.Add(card);
                Str += String.Format(" ({0},{1})", card.MyYBaseCard.Card_TypeID, card.MyYBaseCard.ID);
                p.SoBaiChuaChia.Remove(card);
            }

            SendMessage_GAME_CHIABAI_ToID(p.ID, param.ToArray());
            Console.WriteLine(Str);
        }

        void ParallelChiaBai(SPlayer player)
        {
            int tmp = 0;
            List<YBaseCard> listcard = new List<YBaseCard>();
            List<byte> param = new List<byte>();

            //-----------------------------------------------//
            for (int i = 1; i <= Game.SoLaChiaBanDau; i++)
            {
                tmp = random.Next(0, player.SoBaiChuaChia.Count);
                SCard card = player.SoBaiChuaChia[tmp];
                param.AddRange(BitConverter.GetBytes(card.MyYBaseCard.Card_TypeID));
                param.Add((byte)card.MyYBaseCard.ID);
                listcard.Add(card.MyYBaseCard);
                player.SoBaiTrenTay.Add(card);
                player.SoBaiChuaChia.Remove(card);
            }

            SendMessage_GAME_CHIABAI_ToID(player.ID, param.ToArray());


        }

        /// <summary>
        /// Chia bài cho 2 người chơi
        /// </summary>
        void ChiaBai()
        {
            for(int i=0;i<2;i++)
            {
                //ParallelChiaBai(item);
                Fake_ParallelChiaBai(Players[i]);
            }
            Console.WriteLine("chia bai xong");
        }

        #endregion


        //void ParallelChiaBai(SPlayer player,List<int> ListID)
        //{            
        //    List<YBaseCard> listcard = new List<YBaseCard>();
        //    List<byte> param = new List<byte>();

        //    //-----------------------------------------------//
        //    for (int i = 0; i < ListID.Count; i++)
        //    {
        //        //tmp = random.Next(0, player.SoBaiChuaChia.Count);
        //        SCard card = player.LayCardTrongBoBai(ListID[i]);
        //        param.AddRange(BitConverter.GetBytes(card.MyYBaseCard.Card_TypeID));
        //        param.Add((byte)card.MyYBaseCard.ID);
        //        listcard.Add(card.MyYBaseCard);
        //        player.SoBaiTrenTay.Add(card);
        //        player.SoBaiChuaChia.Remove(card);
        //    }

        //    SendMessage_GAME_CHIABAI_ToID(player.ID, param.ToArray());


        //}

        ///// <summary>
        ///// Chia bài cho 2 người chơi
        ///// </summary>
        //void ChiaBai()
        //{
        //    StreamReader rd = new StreamReader(@ServerSocket.Configuration.PATH_TXT_TEST);
        //    int d = 0;
        //    int total = this.Players[0].SoBaiChuaChia.Count;
        //    foreach (SPlayer item in Players)
        //    {
        //        string[] tmp = rd.ReadLine().Split(",".ToCharArray());
        //        List<int> temp = new List<int>();
        //        foreach (string i in tmp)
        //        {
        //            temp.Add(Int32.Parse(i)+d);
        //        }
        //        ParallelChiaBai(item, temp);
        //        d += total;
        //    }
        //    rd.Close();
        //}


    }
}
